/**
* 版权声明：厦门睿商网络科技有限公司 版权所有 违者必究
* 日    期：2020-05-06
*/
package com.rzico.basics.service;

import com.rzico.base.BaseMapper;
import com.rzico.base.impl.BaseServiceImpl;
import com.rzico.basics.entity.Company;
import com.rzico.basics.entity.CompanyCategory;
import com.rzico.basics.model.CategoryCompanyVo;
import com.rzico.basics.model.CompanyConVo;
import com.rzico.basics.model.CompanyMemberVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.rzico.basics.mapper.CompanyMapper;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <pre>
 * 企业客户业务类
 * </pre>
 *
 * @author Rzico Boot
 * @version 1.0
 */
@Service
public class CompanyService extends BaseServiceImpl<Company, String> {

    @Autowired
    private CompanyMapper companyMapper;

    @Autowired
    private CompanyCategoryService companyCategoryService;

    @Override
    public BaseMapper<Company, String> getMapper() {
        return companyMapper;
    }

    /**
     * 批量删除公司，更新删除状态
     * @param ids
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public int updateDeleteById(Object[] ids) {
        int rw = 0;
        for (Object id:ids) {
            Company company = selectByPrimaryKey(id);
            company.setDeleted(true);
            rw = rw + super.updateByPrimaryKeySelective(company);
        }
        return rw;
    }

    /**
     * 查询公司分类的分类数量
     * @param params
     * @return
     */
    public int countCompanyCategoryNum(Map<String, Object> params) {
        return companyMapper.countCompanyCategoryNum(params);

    }

    /**
     * 恢复已删除的公司
     * @param id 公司id
     * @param companyCategoryId 公司分类id
     * @return
     */
    public Company restoreCompany(Long id, Long companyCategoryId){
        Company company = new Company();
        company.setId(id);
        company.setDeleted(false);
        if (null != companyCategoryId) {
            CompanyCategory companyCategory = companyCategoryService.findById(companyCategoryId);
            if (null == companyCategory) {
                company.setCompanyCategoryId(Long.valueOf("0"));
            } else {
                company.setCompanyCategoryId(companyCategoryId);
            }
        }
        super.updateByPrimaryKeySelective(company);
        Company result = super.findById(company.getId());
        return result;
    }

    /**
     * 展示企业消费信息
     * @param params
     * @return
     */
    public List<CompanyConVo> selectLbsList(Map<String, Object> params){
        return companyMapper.selectLbsList(params);
    }

    /**
     * 统计企业消费总额
     * @param params
     * @return
     */
    public BigDecimal selectLbsListTotalPay(Map<String, Object> params){
        return companyMapper.selectLbsListTotalPay(params);
    }


    /**
     * 查询企业与推荐人的信息
     * @param params
     * @return
     */
    public List<CompanyMemberVo> selectCMList(Map<String, Object> params){
        return companyMapper.selectCMList(params);
    }

}
